'''
在接口测试过程中接口之间经常有依赖关系，比如下面这两个请求一个是设置 Cookie,另外一个是获取 cookie,在没有 Session 保存机制的情况下，第二个接口无法获取第一个接口设置的 Cookie 值。
Request 的会话对象让你能够跨请求保持某些参数。它也会在同一个Session 实例发出的所有请求之间保持 cookie。具体使用如下：
生成会话对象
s = requests.Session()
设置 Cookie
resp = s.get(url1)
获取 Cookie
resp = s.get(url2)
'''
import requests
import re
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
# 案例：登录后，访问个人信息页
id = 1
username = 'zhaojunwei01'
password = '123456'
vericode = 'shtd'

# # 构造 url
url_first = r'http://172.16.22.108:8080/bsams/front/login.do'
url_vericode = r'http://172.16.22.108:8080/bsams/front/vericode.do'
url_info = r'http://172.16.22.108:8080/bsams/front/asset/asset_list.do'
# # 构造 data
data = {
    'taskId': id,
    'loginName': username,
    'password': password,
    'vericode': vericode,
}
#
# # 发送请求
s = requests.Session()
s.get(url_first)
s.get(url_vericode)
s.post(url_first, data=data)
# 访问 资产管理页
resp = s.get(url_info)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)


# 案例：资产管理 - 新增
# url地址构建
url_say = r'http://172.16.22.108:8080/bsams/front/asset/asset_new.do'
# 数据写入
title = 1212,
providerId = 'aaa',
storageId = 'bbb'
data = {
    'title': title,
    'providerId': providerId,
    'storageId': storageId
}
# 发送请求
resp = s.get(url_say,data=data)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)

# 新增 查询
url_say1 = r'http://172.16.22.108:8080/bsams/front/asset/asset_list.do'
data = {
    'status': '全部状态',
    'title': 1212
}
resp = s.post(url_say1)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)

# 修改

url_tesy1 = r'http://172.16.22.108:8080/bsams/front/asset/asset_update.do'
data1 = {
    'title': 111,
    'storageId': '电脑设备库',
    'providerId': '辽宁异界公司'
}

resp = s.get(url_tesy1,data=data1)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)

# 借用
url_lend = r'http://172.16.22.108:8080/bsams/front/asset/asset_borrow.do'
asset_id = {'id': '1713'}
resp = s.get(url_lend, params=asset_id)

data = {
    'id': '1713',
    'departId': '财务部',
    'remark': '苹果11'
}
resp = s.post(url_lend, data=data)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)
# 归还
url_asset = rf'http://172.16.22.108:8080/bsams/front/asset/asset_giveBack.do'
asset_id = {'assetId': '1720'}

resp = s.post(url_asset, data=asset_id)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)
# 查询存放地点
url_deposit = r'http://172.16.22.108:8080/bsams/front/asset_storage/asset_storage_list.do'
resp = s.post(url_deposit)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)
# 查询供应商
url_supplier = r'http://172.16.22.108:8080/bsams/front/asset_provider/asset_provider_list.do'
supplier_data={
     'status': '',
     'title': ''
}
resp = s.post(url_supplier)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)
# 个人信息页面
url_information = r'http://172.16.22.108:8080/bsams/front/asset_user/user_info.do'
resp = s.get(url_information)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)
# 修改手机号
url_phone = r'http://172.16.22.108:8080/bsams/front/asset_user/update_phone.do'
asset_data = {'assetUserId': '26','phone':'18736117546'}
resp = s.post(url_phone,data=asset_data)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)
# 修改密码
url_pwd=r'http://172.16.22.108:8080/bsams/front/asset_user/update_password_pre.do'
pwd_data = {'assetUserId': '123456', 'oldPassword': '123456', 'newPassword': '123456'}
resp = s.post(url_pwd,data=pwd_data)
patt = r'<title>(.*?)</title>'
title = re.findall(patt, resp.text)
print(title)